﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using GeneralMethodClass.Windows;

namespace GeneralMethodClass.Document
{
    public static class FileOP
    {
        /// <summary>
        /// 递归遍历文件夹内所有指定扩展名的文件
        /// </summary>
        /// <param name="folderNamePath">文件夹路径</param>
        /// <param name="extension">文件扩展名</param>
        public static void GetFolderApp(string folderNamePath, string extension)
        {
            string[] foldersPath = Directory.GetDirectories(folderNamePath);
            foreach (string folderPath in foldersPath)
            {
                GetFolderApp(folderPath, extension);
            }

            string[] filesPath = Directory.GetFiles(folderNamePath);
            foreach (string filePath in filesPath)
            {
                FileInfo fileInfo = new FileInfo(filePath);

                //开启后缀为exe的文件
                if (fileInfo.Extension.Equals("." + extension))
                {
                    Process.Start(filePath);
                }
            }
        }

        /// <summary>
        /// 保存文件对话框
        /// </summary>
        /// <param name="fileFilter">文件类型，例如："所以文件|*.*|Excel文件|*.xls"</param>
        /// <param name="index">设置文件类型索引</param>
        /// <returns>True/False</returns>
        public static bool SaveFile(string fileFilter, int index)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = fileFilter;
            saveFile.FilterIndex = index;

            if (saveFile.ShowDialog() == DialogResult.OK)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 打开文件对话框
        /// </summary>
        /// <param name="fileFilter">文件类型，例如："所以文件|*.*|Excel文件|*.xls"</param>
        /// <param name="index">设置文件类型索引</param>
        /// <returns>True/False</returns>
        public static string OpenFile(string fileFilter, int index)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = fileFilter;
            openFile.FilterIndex = index;

            if (openFile.ShowDialog() == DialogResult.OK)
                return openFile.FileName;
            else
                return null;
        }

        /// <summary>
        /// 替换文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="oldPath">源目录</param>
        /// <param name="newPath">目标目录</param>
        public static void ReplaceFile(string fileName, string oldPath, string newPath)
        {
            try
            {
                if (File.Exists(oldPath))
                {
                    File.Delete(oldPath);
                    File.Move(newPath + fileName, oldPath);
                }
                else
                {
                    MsgBox.Show("文件不存在！", "信息");
                }
            }
            catch (Exception ex)
            {
                MsgBox.Show(string.Format("替换文件时出错：{0}", ex.ToString()), "错误");
                return;
            }
        }

        /// <summary>
        /// 获取当前路径
        /// </summary>
        /// <returns></returns>
        public static string CurrentFilePath()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }
    }
}